﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using ZhouYu.NewWorld.BLL.IService;
using ZhouYu.NewWorld.Model;
using ZhouYu.NewWorld.ViewModel;
using ZhouYu.NewWorld.Web.Models;

namespace ZhouYu.NewWorld.Web.Controllers
{
    public class ScenicpicController : Controller
    {
        //注入
        private IScenicpicService _scenicpicService;
        public ScenicpicController(IScenicpicService scenicpicService)
        {
            _scenicpicService = scenicpicService;
        }

        [HttpPost]
        public JsonResult UploadImage(HttpPostedFileBase file)
        {
            //先定义文件
            string fileName = Guid.NewGuid().ToString("N");
            //图片存放的路径
            string filePath = Path.Combine(HttpRuntime.AppDomainAppPath, "Resource/Uploads");
            //如果没有图片
            if (Request.Files.Count == 0)
            {
                return Json(new { Success = false, FilePath = "" });
            }
            //获取图片的扩展名
            string stuf = Path.GetExtension(file.FileName);

            //图片存放路径不存在则创建
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            //保存图片
            string path = Path.Combine(filePath, fileName + stuf);
            file.SaveAs(path);

            //返回前端
            return Json(new { Success = true, FilePath = "/Resource/Uploads/" + fileName + stuf });
        }

        /// <summary>
        /// 获取图片列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public JsonResult GetPicture(int page, int limit)
        {
            //定义表数据总数变量
            int count = 0;
            Expression<Func<Scenicpic, bool>> lambdaCategory = a => true;
            Expression<Func<Scenicpic, int>> orderCategory = a => a.Pic_Id;
            //创建List集合接收业务层返回值
            List<Scenicpic> pics = _scenicpicService.QueryPageList(page, limit, lambdaCategory, orderCategory, out count);
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //框架属性赋值
            responseResult.code = 0;
            responseResult.count = count;
            responseResult.msg = "";
            responseResult.data = pics;
            //返回PageListResult对象
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 获取下级菜单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult GetParent(Scenicpic scenicpic)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            Expression<Func<Scenicpic, bool>> lambdaCategory = a => a.Scenic_Id==scenicpic.Scenic_Id && a.ParentId==0;
            responseResult.data = _scenicpicService.Query(lambdaCategory);
            return Json(responseResult);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 获取切换主图下拉框
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult GetChange(Scenicpic scenicpic)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            Expression<Func<Scenicpic, bool>> lambdaCategory = a => a.Scenic_Id == scenicpic.Scenic_Id && a.ParentId != 0;
            responseResult.data = _scenicpicService.QueryList(lambdaCategory);
            return Json(responseResult);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }



        /// <summary>
        /// 修改是否主图
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult UpdateMaster(int id, short states)
        {
            Expression<Func<Scenicpic, bool>> lambdaCategory = a => a.Pic_Id == id;
            Scenicpic pic = _scenicpicService.Query(lambdaCategory);
            pic.Is_Master = states;
            pic.ModifiedTime = DateTime.Now;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _scenicpicService.Update(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 修改主图
        /// </summary>
        /// <param name="scenicpic"></param>
        /// <returns></returns>
        public JsonResult ChangePic(Scenicpic scenicpic)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            //定义查询表达式
            Expression<Func<Scenicpic, bool>> lambdaCategory1 = a => a.Pic_Id == scenicpic.Pic_Id;
            Expression<Func<Scenicpic, bool>> lambdaCategory = a => a.Scenic_Id==scenicpic.Scenic_Id&&a.Pic_Id!=scenicpic.Pic_Id;
            //获取当前需要修改为主图的图片对象
            Scenicpic pic = _scenicpicService.Query(lambdaCategory1);
            pic.ParentId = 0;
            pic.ModifiedTime = DateTime.Now;
            //获取当前景点下其他图片的集合
            List<Scenicpic> scenicpics = _scenicpicService.QueryList(lambdaCategory);
            //遍历图片集合修改父级ID并修改
            for (int i = 0; i < scenicpics.Count; i++)
            {
                scenicpics[i].ModifiedTime= DateTime.Now;
                scenicpics[i].ParentId = pic.Pic_Id;
                responseResult.Success = _scenicpicService.Update(scenicpics[i]);
                //如果修改失败则Return
                if (!responseResult.Success) 
                {
                    return Json(responseResult);
                }
            }
            //修改主图
            responseResult.Success = _scenicpicService.Update(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }


        /// <summary>
        /// 修改是否有效
        /// </summary>
        /// <param name="id"></param>
        /// <param name="states"></param>
        /// <returns></returns>
        public JsonResult UpdatePicState(int id, short states)
        {
            Expression<Func<Scenicpic, bool>> lambdaCategory = a => a.Pic_Id == id;
            Scenicpic pic = _scenicpicService.Query(lambdaCategory);
            pic.Pic_Status = states;
            pic.ModifiedTime = DateTime.Now;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _scenicpicService.Update(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult AddScenicpic(Scenicpic scenicpic)
        {
            scenicpic.ModifiedTime = DateTime.Now;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _scenicpicService.Add(scenicpic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult DeletePicture(Scenicpic pic)
        {
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _scenicpicService.Delete(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        /// <summary>
        /// 编辑图片
        /// </summary>
        /// <param name="pic"></param>
        /// <returns></returns>
        public JsonResult EditScenicpic(Scenicpic pic)
        {
            pic.ModifiedTime = DateTime.Now;
            //创建返回结果对象
            ResponseResult responseResult = new ResponseResult();
            responseResult.Success = _scenicpicService.Update(pic);
            return Json(responseResult, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet参数：防止数据不安全泄露
        }

        

    }
}